home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Database / BooleanFormatter / BooleanFormatter.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  171 lines

  1. /*
  2. *    BooleanFormatter.h
  3. *              Original code: William Shipley
  4. *            Revised for string display: Mai Nguyen
  5. */
  6.  
  7. #import "BooleanFormatter.h"
  8.  
  9. #define _delWill1    @selector(formatterWillChangeValueFor:at:sender:)
  10. #define _delWill2    @selector(formatterWillChangeValueFor:at:to:sender:)
  11. #define _delDid        @selector(formatterDidChangeValueFor:at:to:sender:)
  12.  
  13. #define TEXT_OFFSET    3.0
  14.  
  15. @implementation BooleanFormatter
  16.  
  17. - init
  18. {
  19.     font = [Font newFont:"Helvetica" size:12.0];
  20.     newValue = [[DBValue allocFromZone:[self zone]] init];
  21.     return[super init];
  22. }
  23.  
  24. - free
  25. {
  26.     [newValue free];
  27.     return[super free];
  28. }
  29.  
  30.  
  31. - drawFieldAt:(unsigned int)row :(unsigned int)column
  32.     inside:(NXRect *)frame inView:view
  33.     withAttributes:(id <DBTableVectors >)rowAttrs
  34.      :(id <DBTableVectors >)columnAttrs
  35.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos;
  36. {
  37.  
  38.     NXPoint             toPoint;
  39.     NXSize              string;
  40.     char                buf[5];
  41.     
  42.     [self getValueAt:row :column withAttributes:rowAttrs :columnAttrs
  43.      usePositions:useRowPos :useColumnPos];
  44.  
  45.  /*
  46.   * set proper string
  47.   */
  48.     if ([value isNull] || ([value intValue] == 0))
  49.         sprintf(buf, "%s", "NO");
  50.     else
  51.         sprintf(buf,"%s", "YES");
  52.     
  53.  /*
  54.   * Set the size on the font.
  55.   */
  56.       [font set];    
  57.     string.height = [font pointSize];
  58.     string.width = [font getWidthOf:buf];
  59.  
  60.     
  61.     toPoint.y = frame->origin.y + frame->size.height -
  62.       (frame->size.height - string.height) / 2;
  63.     toPoint.x = frame->origin.x +
  64.       (frame->size.width - string.width) / 2 - TEXT_OFFSET;
  65.     
  66.        PSmoveto(toPoint.x, toPoint.y);
  67.     PSsetgray(NX_BLACK);
  68.     PSshow(buf);
  69.     return self;
  70. }
  71.  
  72. - mouseDown:(NXEvent *)theEvent at:(int)row :(int)column
  73.     inside:(NXRect *)frame inView:(View *) view
  74.     withAttributes:(id <DBTableVectors >)rowAttrs
  75.      :(id <DBTableVectors >)columnAttrs
  76.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos
  77. {
  78.     if (theEvent->data.mouse.click < 2)
  79.     return nil;
  80.  
  81.      /* get the current value */
  82.  
  83.     [self getValueAt:row :column withAttributes:rowAttrs :columnAttrs
  84.      usePositions:useRowPos :useColumnPos];
  85.  
  86.     /* Toggle value */
  87.     if ([value isNull] || ([value intValue] == 0))
  88.         [newValue setIntValue:1];
  89.     else
  90.         [newValue setIntValue:0];
  91.  
  92.     [self setValueAt:row :column
  93.      withAttributes:rowAttrs :columnAttrs
  94.      usePositions:useRowPos :useColumnPos];
  95.    
  96.  
  97.  /* Redraw the modified field */
  98.     [self drawFieldAt:row :column
  99.      inside:frame inView:view
  100.      withAttributes:rowAttrs :columnAttrs
  101.      usePositions:useRowPos :useColumnPos];
  102.     [[view window] flushWindow];
  103.  
  104.     return self;
  105. }
  106.  
  107.  
  108. - setValueAt:(int)row :(int)column
  109.     withAttributes:(id <DBTableVectors >)rowAttrs
  110.      :(id <DBTableVectors >)columnAttrs
  111.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos
  112. {
  113.     BOOL                isChangeOk = YES;
  114.  
  115.  /* Ask delegate if we can change */
  116.     if (useRowPos && !useColumnPos && delWill2)
  117.     isChangeOk = [delegate formatterWillChangeValueFor:
  118.               [columnAttrs identifier]
  119.               at:row to:newValue sender:self];
  120.     else if (useColumnPos && !useRowPos && delWill2)
  121.     isChangeOk = [delegate formatterWillChangeValueFor:
  122.               [rowAttrs identifier]
  123.               at:column to:newValue sender:self];
  124.     if (!isChangeOk)
  125.     return nil;
  126.  
  127.  /* Tell dataSource our newValue */
  128.     if (useRowPos && !useColumnPos)
  129.     [dataSource setValueFor:[columnAttrs identifier] at:row
  130.      from:newValue];
  131.     else if (useColumnPos && !useRowPos)
  132.     [dataSource setValueFor:[rowAttrs identifier] at:column
  133.      from:newValue];
  134.    
  135.  /* Tell delegate we changed */
  136.     if (useRowPos && !useColumnPos && delDid)
  137.     [delegate formatterDidChangeValueFor:[columnAttrs identifier]
  138.      at:row to:newValue sender:self];
  139.     else if (!useRowPos && useColumnPos && delDid)
  140.     [delegate formatterDidChangeValueFor:[rowAttrs identifier]
  141.      at:column to:newValue sender:self];
  142.     return self;
  143. }
  144.  
  145. - setDelegateFlags
  146. {
  147.     delWill1 = delWill2 = delDid = NO;
  148.  
  149.     if ([delegate respondsTo:_delWill1])
  150.     delWill1 = YES;
  151.     if ([delegate respondsTo:_delWill2])
  152.     delWill2 = YES;
  153.     if ([delegate respondsTo:_delDid])
  154.     delDid = YES;
  155.     return self;
  156. }
  157.  
  158. - setDelegate:newDelegate
  159. {
  160.     if (newDelegate == delegate)
  161.     return self;
  162.     else
  163.     [super setDelegate:newDelegate];
  164.  
  165.     [self setDelegateFlags];
  166.  
  167.     return self;
  168. }
  169.  
  170. @end
  171.